package com.zxf.appliction.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zxf.appliction.util.Verification;


@RequestMapping("/verification")
@Controller
public class VerificationController {
	private static final long serialVersionUID = 1L;

	private ByteArrayInputStream bais;

	public ByteArrayInputStream getBais() {
		return bais;
	}

	public void setBais(ByteArrayInputStream bais) {
		this.bais = bais;
	}

	private static Verification verification;
	String strCode = null;

	// private Map session ;

	@RequestMapping(value = "/getCode")
	public @ResponseBody
	void getCode(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		HttpSession session = request.getSession();
		// obj.put("success", true);
		bais = verification.createImage();
		strCode = verification.strCode;
		  //生成缓冲区image类
        BufferedImage image;
		try {
			image = ImageIO.read(bais);
			ImageIO.write(image, "JPEG", response.getOutputStream());
			 response.getOutputStream().flush();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		session.setAttribute("randomCode", strCode);
		System.out.println("YanZhengMaAction>randomCode:" + strCode);
		// 设置response头信息
		// 禁止缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// 放入session，在login的时候需要用到验证
	
		//obj.put("success", true);
		//obj.put("ddd", "好好的！");
		//obj.put("randomCode", strCode);
		//System.out.println(obj.toString());
		
      
		//return obj;
	}

}
